﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace GenerateAdminPage.Classes.FrmClasses
{
    #region USING
    using GenerateAdminPage.Classes.Base;
    using GenerateAdminPage.Classes.Helpers;
    using GenerateAdminPage.Classes.DBStructure;
    #endregion

    public class FrmClassesSingle : AbstractFrmClass
    {
        public override string GenerateClass()
        {
            StringBuilder Result = new StringBuilder();

            Result.AppendLine(TAB + "public partial class FrmQuanLy" + TblOne.Name + " : Office2007Form");
            Result.AppendLine(TAB + "{");

            Result.AppendLine(GenerateVariables());
            Result.AppendLine(GenerateConstructor());
            Result.AppendLine(GenerateFormLoad());
            Result.AppendLine(GeneratebtnChon_Click());
            Result.AppendLine(GeneratebtnThem_Click());
            Result.AppendLine(GeneratebtnXoa_Click());
            Result.AppendLine(GeneratebtnCapNhat_Click());
            Result.AppendLine(GeneratebtnThoat_Click());
            Result.AppendLine(GeneratebtnReset_Click());
            Result.AppendLine(GeneratebtndtgvData_CellClick());
            Result.AppendLine(GenerateLoadDetailInfo());
            Result.AppendLine(GenerateInitGridView());
            Result.AppendLine(GenerateInitHiddenFields());
            Result.AppendLine(GenerateLoadData());
            Result.AppendLine(GenerateValidateInput());
            Result.AppendLine(GenerateActionAdd());
            Result.AppendLine(GenerateActionUpdate());
            Result.AppendLine(GenerateActionDelete());
            Result.AppendLine(GenerateTransferDataInfo());
            Result.AppendLine(GeneratebtnChonThamChieu_Click());
            Result.AppendLine(GeneratebtnChonHinhAnh());

            Result.AppendLine(TAB + "}");

            return Result.ToString();
        }

        public string GeneratebtnChonHinhAnh()
        {
            StringBuilder Result = new StringBuilder();

            for (int i = 0; i < TblOne.Attributes.Count; i++)
            {
                if (TblOne.Attributes[i].Type == DataType.IMAGE)
                {
                    Result.AppendLine(TAB2 + "private void btnChon" + TblOne.Attributes[i].Name + "_Click(object sender, EventArgs e)");
                    Result.AppendLine(TAB2 + "{");
                    Result.AppendLine(TAB3 + "if (openFileDialog1.ShowDialog() == DialogResult.OK)");
                    Result.AppendLine(TAB3 + "{");
                    Result.AppendLine(TAB4 + "ptb" + TblOne.Attributes[i].Name + ".Image = new Bitmap(openFileDialog1.FileName);");
                    Result.AppendLine(TAB3 + "}");
                    Result.AppendLine(TAB2 + "}" + END);

                    Result.AppendLine(TAB2 + "private void btnXoa" + TblOne.Attributes[i].Name + "_Click(object sender, EventArgs e)");
                    Result.AppendLine(TAB2 + "{");
                    Result.AppendLine(TAB3 + "ptb" + TblOne.Attributes[i].Name + ".Image = null;");
                    Result.AppendLine(TAB2 + "}" + END);
                }
            }

            return Result.ToString();
        }

        public string GeneratebtnChonThamChieu_Click()
        {
            StringBuilder Result = new StringBuilder();

            for (int i = 0; i < TblOne.Attributes.Count; i++)
            {
                if (TblOne.Attributes[i].IsForeignKey)
                {
                    Result.AppendLine(TAB2 + "private void btnChon" + TblOne.Attributes[i].ReferTo + "_Click(object sender, EventArgs e)");
                    Result.AppendLine(TAB2 + "{");
                    Result.AppendLine(TAB3 + "FrmQuanLy" + TblOne.Attributes[i].ReferTo + " frm = new FrmQuanLy" + TblOne.Attributes[i].ReferTo + "(true);");
                    Result.AppendLine(TAB3 + "frm.Handler += Get" + TblOne.Attributes[i].ReferTo + ";");
                    Result.AppendLine(TAB3 + "frm.ShowDialog();");
                    Result.AppendLine(TAB2 + "}" + END);
                    Result.AppendLine(GenerateGetThamChieu(Utils.GetTableByName(DB, TblOne.Attributes[i].ReferTo)));
                }
            }

            return Result.ToString();
        }

        public string GenerateGetThamChieu(Table _tbl)
        {
            StringBuilder Result = new StringBuilder();

            Result.AppendLine(TAB2 + "public void Get" + _tbl.Name + "(object sender, EventArgs e)");
            Result.AppendLine(TAB2 + "{");
            Result.AppendLine(TAB3 + "var eventType = (TransferSimpleDataEvent)e;");
            Result.AppendLine(TAB3 + "string[] comp = eventType.Data.Split(new char[] { '#' });");
            Result.AppendLine(TAB3 + "txt" + Utils.GetPK(_tbl).Name + ".Text = comp[0];");
            Result.AppendLine(TAB3 + "txt" + _tbl.Name + ".Text = comp[1];");
            Result.AppendLine(TAB2 + "}");

            return Result.ToString();
        }

        public string GenerateVariables()
        {
            StringBuilder Result = new StringBuilder();

            if (Utils.HaveFKReferTo(DB, TblOne))
            {
                Result.AppendLine(TAB2 + "public EventHandler Handler { get; set; }");
            }

            for (int i = 0; i < TblOne.Attributes.Count; i++)
            {
                if (TblOne.Attributes[i].IsForeignKey)
                {
                    Result.AppendLine(TAB2 + "private DevComponents.DotNetBar.Controls.TextBoxX txt" + TblOne.Attributes[i].Name + ";");
                }
            }

            return Result.ToString();
        }

        public string GenerateInitHiddenFields()
        {
            StringBuilder Result = new StringBuilder();
            var found = false;

            for (int i = 0; i < TblOne.Attributes.Count; i++)
            {
                if (TblOne.Attributes[i].IsForeignKey)
                {
                    found = true;
                    break;
                }
            }

            if (!found)
            {
                return Result.ToString();
            }

            Result.AppendLine(TAB2 + "public void InitHiddenFields()");
            Result.AppendLine(TAB2 + "{");
            
            for (int i = 0; i < TblOne.Attributes.Count; i++)
            {
                if (TblOne.Attributes[i].IsForeignKey)
                {
                    Result.AppendLine(TAB3 + "txt" + TblOne.Attributes[i].Name + " = new DevComponents.DotNetBar.Controls.TextBoxX");
                    Result.AppendLine(TAB3 + "{");
                    Result.AppendLine(TAB4 + "Name = \"txt" + TblOne.Attributes[i].Name + "\",");
                    Result.AppendLine(TAB4 + "Text = \"\"");
                    Result.AppendLine(TAB3 + "};");
                    Result.AppendLine(TAB3 + "txt" + TblOne.Attributes[i].Name + ".Visible = false;");
                }
            }

            Result.AppendLine(TAB2 + "}");
            return Result.ToString();
        }

        public string GenerateConstructor()
        {
            StringBuilder Result = new StringBuilder();

            Result.AppendLine(TAB2 + "public FrmQuanLy" + TblOne.Name + "(bool enableChon = false)");
            Result.AppendLine(TAB2 + "{");
            Result.AppendLine(TAB3 + "InitializeComponent();");
            if (Utils.HaveFKReferTo(DB, TblOne))
            {
                Result.AppendLine(TAB3 + "btnChon.Enabled = enableChon;");
            }
            Result.AppendLine(TAB2 + "}");

            return Result.ToString();
        }

        public string GenerateFormLoad()
        {
            StringBuilder Result = new StringBuilder();

            Result.AppendLine(TAB2 + "private void FrmQuanLy" + TblOne.Name + "_Load(object sender, EventArgs e)");
            Result.AppendLine(TAB2 + "{");
            Result.AppendLine(TAB3 + "InitGridView();");
            if (Utils.TableHaveForeignKey(TblOne))
            {
                Result.AppendLine(TAB3 + "InitHiddenFields();");
            }
            Result.AppendLine(TAB3 + "LoadData();");
            Result.AppendLine(TAB3 + "dtgvData.ClearSelection();");
            Result.AppendLine(TAB2 + "}");

            return Result.ToString();
        }

        public string GeneratebtnChon_Click()
        {
            StringBuilder Result = new StringBuilder();

            if (Utils.HaveFKReferTo(DB, TblOne))
            {
                Result.AppendLine(TAB2 + "private void btnChon_Click(object sender, EventArgs e)");
                Result.AppendLine(TAB2 + "{");
                Result.AppendLine(TAB3 + "var errorText = \"\";");
                Result.AppendLine(TAB3 + "if (!ValidateInput(EnumUpdateMode.CHOOSING, ref errorText))");
                Result.AppendLine(TAB3 + "{");
                Result.AppendLine(TAB4 + "MessageBox.Show(errorText, \"Lỗi\", MessageBoxButtons.OK, MessageBoxIcon.Error);");
                Result.AppendLine(TAB4 + "return;");
                Result.AppendLine(TAB3 + "}");
                Result.AppendLine(TAB3 + "var id = int.Parse(txt" + Utils.GetPK(TblOne).Name + ".Text);");
                Result.AppendLine(TAB3 + "var name = " + TblOne.Name + "Repository.SelectByID(id)." + Utils.GetStrAttributeStartWithTen(TblOne) + ";");
                Result.AppendLine(TAB3 + "TransferDataInfo(this, new TransferSimpleDataEvent(id + \"#\" + name));");
                Result.AppendLine(TAB2 + "}");
            }
            return Result.ToString();
        }

        public string GenerateTransferDataInfo()
        {
            StringBuilder Result = new StringBuilder();

            if (Utils.HaveFKReferTo(DB, TblOne))
            {
                Result.AppendLine(TAB2 + "public void TransferDataInfo(object sender, TransferSimpleDataEvent e)");
                Result.AppendLine(TAB2 + "{");
                Result.AppendLine(TAB3 + "this.Close();");
                Result.AppendLine(TAB3 + "this.Handler(this, e);");
                Result.AppendLine(TAB2 + "}");
            }
            return Result.ToString();
        }

        public string GeneratebtnThem_Click()
        {
            StringBuilder Result = new StringBuilder();

            Result.AppendLine(TAB2 + "private void btnThem_Click(object sender, EventArgs e)");
            Result.AppendLine(TAB2 + "{");
            Result.AppendLine(TAB3 + "var errorText = \"\";");
            Result.AppendLine(TAB3 + "if (!ValidateInput(EnumUpdateMode.INSERT, ref errorText))");
            Result.AppendLine(TAB3 + "{");
            Result.AppendLine(TAB4 + "MessageBoxEx.Show(errorText, \"Lỗi\", MessageBoxButtons.OK, MessageBoxIcon.Error);");
            Result.AppendLine(TAB4 + "return;");
            Result.AppendLine(TAB3 + "}");
            Result.AppendLine(TAB3 + "if (ActionAdd())");
            Result.AppendLine(TAB3 + "{");
            Result.AppendLine(TAB4 + "MessageBoxEx.Show(\"Lưu dữ liệu thành công\", \"Thông báo\", MessageBoxButtons.OK, MessageBoxIcon.Information);");
            Result.AppendLine(TAB4 + "LoadData();");
            Result.AppendLine(TAB4 + "btnReset_Click(sender, e);");
            Result.AppendLine(TAB3 + "}");
            Result.AppendLine(TAB3 + "else");
            Result.AppendLine(TAB3 + "{");
            Result.AppendLine(TAB4 + "MessageBoxEx.Show(\"Lưu dữ liệu thất bại\", \"Lỗi\", MessageBoxButtons.OK, MessageBoxIcon.Error);");
            Result.AppendLine(TAB3 + "}");
            Result.AppendLine(TAB2 + "}");

            return Result.ToString();
        }

        public string GeneratebtnXoa_Click()
        {
            StringBuilder Result = new StringBuilder();

            Result.AppendLine(TAB2 + "private void btnXoa_Click(object sender, EventArgs e)");
            Result.AppendLine(TAB2 + "{");
            Result.AppendLine(TAB3 + "var errorText = \"\";");
            Result.AppendLine(TAB3 + "if (!ValidateInput(EnumUpdateMode.DELETE, ref errorText))");
            Result.AppendLine(TAB3 + "{");
            Result.AppendLine(TAB4 + "MessageBoxEx.Show(errorText, \"Lỗi\", MessageBoxButtons.OK, MessageBoxIcon.Error);");
            Result.AppendLine(TAB4 + "return;");
            Result.AppendLine(TAB3 + "}");
            Result.AppendLine(TAB3 + "if (MessageBoxEx.Show(\"Bạn có chắc chắn xóa dòng này không?\", \"Xóa\", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)");
            Result.AppendLine(TAB3 + "{");
            Result.AppendLine(TAB4 + "if (ActionDelete())");
            Result.AppendLine(TAB4 + "{");
            Result.AppendLine(TAB5 + "MessageBoxEx.Show(\"Xóa dữ liệu thành công\", \"Thông báo\", MessageBoxButtons.OK, MessageBoxIcon.Information);");
            Result.AppendLine(TAB5 + "LoadData();");
            Result.AppendLine(TAB5 + "btnReset_Click(sender, e);");
            Result.AppendLine(TAB4 + "}");
            Result.AppendLine(TAB4 + "else");
            Result.AppendLine(TAB4 + "{");
            Result.AppendLine(TAB5 + "MessageBoxEx.Show(\"Xóa dữ liệu thất bại\", \"Lỗi\", MessageBoxButtons.OK, MessageBoxIcon.Error);");
            Result.AppendLine(TAB4 + "}");
            Result.AppendLine(TAB3 + "}");
            Result.AppendLine(TAB2 + "}");

            return Result.ToString();
        }

        public string GeneratebtnCapNhat_Click()
        {
            StringBuilder Result = new StringBuilder();

            Result.AppendLine(TAB2 + "private void btnCapNhat_Click(object sender, EventArgs e)");
            Result.AppendLine(TAB2 + "{");
            Result.AppendLine(TAB3 + "var errorText = \"\";");
            Result.AppendLine(TAB3 + "if (!ValidateInput(EnumUpdateMode.UPDATE, ref errorText))");
            Result.AppendLine(TAB3 + "{");
            Result.AppendLine(TAB4 + "MessageBoxEx.Show(errorText, \"Lỗi\", MessageBoxButtons.OK, MessageBoxIcon.Error);");
            Result.AppendLine(TAB4 + "return;");
            Result.AppendLine(TAB3 + "}");
            Result.AppendLine(TAB3 + "if (ActionUpdate())");
            Result.AppendLine(TAB3 + "{");
            Result.AppendLine(TAB4 + "MessageBoxEx.Show(\"Cập nhật dữ liệu thành công\", \"Thông báo\", MessageBoxButtons.OK, MessageBoxIcon.Information);");
            Result.AppendLine(TAB4 + "LoadData();");
            Result.AppendLine(TAB3 + "}");
            Result.AppendLine(TAB3 + "else");
            Result.AppendLine(TAB3 + "{");
            Result.AppendLine(TAB4 + "MessageBoxEx.Show(\"Cập nhật dữ liệu thất bại\", \"Lỗi\", MessageBoxButtons.OK, MessageBoxIcon.Error);");
            Result.AppendLine(TAB3 + "}");
            Result.AppendLine(TAB2 + "}");

            return Result.ToString();
        }

        public string GeneratebtnThoat_Click()
        {
            StringBuilder Result = new StringBuilder();

            Result.AppendLine(TAB2 + "private void btnThoat_Click(object sender, EventArgs e)");
            Result.AppendLine(TAB2 + "{");
            Result.AppendLine(TAB3 + "this.Close();");
            Result.AppendLine(TAB2 + "}");

            return Result.ToString();
        }

        public string GeneratebtnReset_Click()
        {
            StringBuilder Result = new StringBuilder();

            Result.AppendLine(TAB2 + "private void btnReset_Click(object sender, EventArgs e)");
            Result.AppendLine(TAB2 + "{");
            for (int i = 0; i < TblOne.Attributes.Count; i++)
            {
                if (TblOne.Attributes[i].IsForeignKey)
                {
                    Result.AppendLine(TAB3 + "txt" + TblOne.Attributes[i].Name + ".Text = \"\";");
                    Result.AppendLine(TAB3 + "txt" + TblOne.Attributes[i].ReferTo + ".Text = \"\";");
                }
                else
                {
                    if (TblOne.Attributes[i].Type == DataType.DATETIME)
                    {
                        Result.AppendLine(TAB3 + "dt" + TblOne.Attributes[i].Name + ".Value = DateTime.MinValue;");
                    }
                    else if (TblOne.Attributes[i].Type == DataType.BOOL)
                    {
                        Result.AppendLine(TAB3 + "cbx" + TblOne.Attributes[i].Name + ".SelectedIndex = -1;");
                    }
                    else if (TblOne.Attributes[i].Type == DataType.IMAGE)
                    {
                        Result.AppendLine(TAB3 + "ptb" + TblOne.Attributes[i].Name + ".Image = null;");
                    }
                    else
                    {
                        Result.AppendLine(TAB3 + "txt" + TblOne.Attributes[i].Name + ".Text = \"\";");
                    }
                }
            }
            Result.AppendLine(TAB2 + "}");

            return Result.ToString();
        }

        public string GeneratebtndtgvData_CellClick()
        {
            StringBuilder Result = new StringBuilder();

            Result.AppendLine(TAB2 + "private void dtgvData_CellClick(object sender, DataGridViewCellEventArgs e)");
            Result.AppendLine(TAB2 + "{");
            Result.AppendLine(TAB3 + "if (dtgvData.SelectedRows == null || dtgvData.SelectedRows.Count == 0)");
            Result.AppendLine(TAB4 + "return;");
            Result.AppendLine(TAB3 + "var selectedItem = dtgvData.SelectedRows[0];");
            Result.AppendLine(TAB3 + "txt" + Utils.GetPK(TblOne).Name + ".Text = dtgvData.SelectedRows[0].Cells[\"" + Utils.GetPK(TblOne).Name + "\"].Value.ToString();");
            Result.AppendLine(TAB3 + "var item = " + TblOne.Name + "Repository.SelectByID(" + Utils.BuildCastingPK(TblOne) + ");");
            Result.AppendLine(TAB3 + "LoadDetailInfo(item);");
            Result.AppendLine(TAB2 + "}");

            return Result.ToString();
        }

        public string GenerateLoadDetailInfo()
        {
            StringBuilder Result = new StringBuilder();

            Result.AppendLine(TAB2 + "public void LoadDetailInfo(" + TblOne.Name + " item)");
            Result.AppendLine(TAB2 + "{");
            for (int i = 0; i < TblOne.Attributes.Count; i++)
            {
                if (!TblOne.Attributes[i].IsPrimaryKey)
                {
                    if (TblOne.Attributes[i].IsForeignKey)
                    {
                        Result.AppendLine(TAB3 + "txt" + TblOne.Attributes[i].Name + ".Text = item." + TblOne.Attributes[i].Name + " == null ? \"\" : item." + TblOne.Attributes[i].Name + ".ToString();");
                        Result.AppendLine(TAB3 + "txt" + TblOne.Attributes[i].ReferTo + ".Text = item." + TblOne.Attributes[i].ReferTo + " == null ? \"\" : item." + TblOne.Attributes[i].ReferTo + "." + Utils.GetStrAttributeStartWithTen(Utils.GetTableByName(DB, TblOne.Attributes[i].ReferTo)) + ".ToString();");
                    }
                    else
                    {
                        if (TblOne.Attributes[i].Type == DataType.DATETIME)
                        {
                            Result.AppendLine(TAB3 + "dt" + TblOne.Attributes[i].Name + ".Value = item." + TblOne.Attributes[i].Name + " == null ? DateTime.MinValue : item." + TblOne.Attributes[i].Name + ".Value;");
                        }
                        else if (TblOne.Attributes[i].Type == DataType.BOOL)
                        {
                            Result.AppendLine(TAB3 + "cbx" + TblOne.Attributes[i].Name + ".SelectedIndex = item." + TblOne.Attributes[i].Name + " == null ? -1 : (item." + TblOne.Attributes[i].Name + ".Value ? 0 : 1);");
                        }
                        else if (TblOne.Attributes[i].Type == DataType.IMAGE)
                        {
                            Result.AppendLine(TAB3 + "if (item." + TblOne.Attributes[i].Name + "!= null)");
                            Result.AppendLine(TAB3 + "{");
                            Result.AppendLine(TAB4 + "try");
                            Result.AppendLine(TAB4 + "{");
                            Result.AppendLine(TAB5 + "Image newImage;");
                            Result.AppendLine(TAB5 + "using (MemoryStream ms = new MemoryStream(item." + TblOne.Attributes[i].Name + ", 0, item." + TblOne.Attributes[i].Name + ".Length))");
                            Result.AppendLine(TAB5 + "{");
                            Result.AppendLine(TAB6 + "ms.Write(item." + TblOne.Attributes[i].Name + ", 0, item." + TblOne.Attributes[i].Name + ".Length);");
                            Result.AppendLine(TAB6 + "newImage = Image.FromStream(ms, true);");
                            Result.AppendLine(TAB5 + "}");
                            Result.AppendLine(TAB5 + "ptb" + TblOne.Attributes[i].Name + ".Image = newImage;");
                            Result.AppendLine(TAB4 + "}");
                            Result.AppendLine(TAB4 + "catch (Exception ex)");
                            Result.AppendLine(TAB4 + "{");
                            Result.AppendLine(TAB5 + "MessageBox.Show(ex.ToString());");
                            Result.AppendLine(TAB4 + "}");
                            Result.AppendLine(TAB3 + "}");
                        }
                        else
                        {
                            Result.AppendLine(TAB3 + "txt" + TblOne.Attributes[i].Name + ".Text = item." + TblOne.Attributes[i].Name + " == null ? \"\" : item." + TblOne.Attributes[i].Name + ".ToString();");
                        }
                    }
                }
            }
            Result.AppendLine(TAB2 + "}");

            return Result.ToString();
        }

        public string GenerateInitGridView()
        {
            StringBuilder Result = new StringBuilder();

            Result.AppendLine(TAB2 + "private void InitGridView()");
            Result.AppendLine(TAB2 + "{");

            for (int i = 0; i < TblOne.Attributes.Count; i++)
            {
                if (i == 0)
                {
                    Result.AppendLine(TAB3 + "DataGridViewTextBoxColumn objColumn = new DataGridViewTextBoxColumn");
                }
                else
                {
                    Result.AppendLine(TAB3 + "objColumn = new DataGridViewTextBoxColumn");
                }
                Result.AppendLine(TAB3 + "{");
                Result.AppendLine(TAB4 + "HeaderText = \"" + TblOne.Attributes[i].Name + "\",");
                Result.AppendLine(TAB4 + "DataPropertyName = \"" + TblOne.Attributes[i].Name + "\",");
                Result.AppendLine(TAB4 + "Name = \"" + TblOne.Attributes[i].Name + "\",");

                if (i < TblOne.Attributes.Count - 1)
                {
                    Result.AppendLine(TAB4 + "Width = (int)((dtgvData.Width - dtgvData.RowHeadersWidth)*((double)1/" + TblOne.Attributes.Count + "))");
                }
                else
                {
                    Result.AppendLine(TAB4 + "Width = (int)((dtgvData.Width - dtgvData.RowHeadersWidth)*((double)1/" + TblOne.Attributes.Count + ")) - 1");
                }

                Result.AppendLine(TAB3 + "};");
                Result.AppendLine(TAB3 + "dtgvData.Columns.Add(objColumn);");
            }

            Result.AppendLine(TAB2 + "}");

            return Result.ToString();
        }

        public string GenerateLoadData()
        {
            StringBuilder Result = new StringBuilder();

            Result.AppendLine(TAB2 + "private void LoadData()");
            Result.AppendLine(TAB2 + "{");
            Result.AppendLine(TAB3 + "var lstItem = " + TblOne.Name + "Repository.SelectAll();");
            Result.AppendLine(TAB3 + "var dt = new DataTable();");

            for (int i = 0; i < TblOne.Attributes.Count; i++)
            {
                Result.AppendLine(TAB3 + "dt.Columns.Add(\"" + TblOne.Attributes[i].Name + "\");");
            }

            Result.AppendLine(TAB3 + "for (int i = 0; i < lstItem.Count; i++)");
            Result.AppendLine(TAB3 + "{");
            Result.AppendLine(TAB4 + "dt.Rows.Add(" + Utils.BuildListColumnData(TblOne.Attributes) + ");");
            Result.AppendLine(TAB3 + "}");
            Result.AppendLine(TAB3 + "DataView objTimeSheetDV = new System.Data.DataView(dt);");
            Result.AppendLine(TAB3 + "objTimeSheetDV.AllowNew = false;");
            Result.AppendLine(TAB3 + "dtgvData.AutoGenerateColumns = false;");
            Result.AppendLine(TAB3 + "dtgvData.DataSource = objTimeSheetDV;");
            Result.AppendLine(TAB3 + "dtgvData.ClearSelection();");
            Result.AppendLine(TAB2 + "}");

            return Result.ToString();
        }

        public string GenerateValidateInput()
        {
            StringBuilder Result = new StringBuilder();

            Result.AppendLine(TAB2 + "private bool ValidateInput(EnumUpdateMode mode, ref string errorText)");
            Result.AppendLine(TAB2 + "{");
            Result.AppendLine(TAB3 + "if (mode == EnumUpdateMode.UPDATE || mode == EnumUpdateMode.DELETE)");
            Result.AppendLine(TAB3 + "{");
            Result.AppendLine(TAB4 + "if (txt" + Utils.GetPK(TblOne).Name + ".Text == \"\")");
            Result.AppendLine(TAB4 + "{");
            Result.AppendLine(TAB5 + "errorText = \"Vui lòng chọn " + Utils.GetPK(TblOne).Name + "\";");
            Result.AppendLine(TAB5 + "return false;");
            Result.AppendLine(TAB4 + "}");
            Result.AppendLine(TAB3 + "}");
            Result.AppendLine(TAB3 + "if (mode != EnumUpdateMode.DELETE)");
            Result.AppendLine(TAB3 + "{");

            for (int i = 0; i < TblOne.Attributes.Count; i++)
            {
                if (!TblOne.Attributes[i].IsPrimaryKey || (TblOne.Attributes[i].IsPrimaryKey && !TblOne.Attributes[i].IsIdentify))
                {
                    if (TblOne.Attributes[i].Type == DataType.DATETIME)
                    {
                        Result.AppendLine(TAB4 + "if (dt" + TblOne.Attributes[i].Name + ".Value == DateTime.MinValue)");
                        Result.AppendLine(TAB4 + "{");
                        Result.AppendLine(TAB5 + " errorText = \"Vui lòng chọn " + TblOne.Attributes[i].Name + "\";");
                        Result.AppendLine(TAB5 + "return false;");
                        Result.AppendLine(TAB4 + "}");
                    }
                    else if (TblOne.Attributes[i].Type == DataType.BOOL)
                    {
                        Result.AppendLine(TAB4 + "if (cbx" + TblOne.Attributes[i].Name + ".SelectedIndex == -1)");
                        Result.AppendLine(TAB4 + "{");
                        Result.AppendLine(TAB5 + " errorText = \"Vui lòng chọn " + TblOne.Attributes[i].Name + "\";");
                        Result.AppendLine(TAB5 + "return false;");
                        Result.AppendLine(TAB4 + "}");
                    }
                    else if (TblOne.Attributes[i].Type == DataType.IMAGE)
                    {
                        Result.AppendLine(TAB4 + "if (ptb" + TblOne.Attributes[i].Name + ".Image == null)");
                        Result.AppendLine(TAB4 + "{");
                        Result.AppendLine(TAB5 + " errorText = \"Vui lòng chọn " + TblOne.Attributes[i].Name + "\";");
                        Result.AppendLine(TAB5 + "return false;");
                        Result.AppendLine(TAB4 + "}");
                    }
                    else
                    {
                        Result.AppendLine(TAB4 + "if (txt" + TblOne.Attributes[i].Name + ".Text == \"\")");
                        Result.AppendLine(TAB4 + "{");
                        Result.AppendLine(TAB5 + " errorText = \"Vui lòng nhập " + TblOne.Attributes[i].Name + "\";");
                        Result.AppendLine(TAB5 + "return false;");
                        Result.AppendLine(TAB4 + "}");
                    }
                }
            }

            Result.AppendLine(TAB3 + "}");
            Result.AppendLine(TAB3 + "return true;");
            Result.AppendLine(TAB2 + "}");

            return Result.ToString();
        }

        public string GenerateActionAdd()
        {
            StringBuilder Result = new StringBuilder();
            var found = false;

            Result.AppendLine(TAB2 + "private bool ActionAdd()");
            Result.AppendLine(TAB2 + "{");
            Result.AppendLine(TAB3 + "try");
            Result.AppendLine(TAB3 + "{");
            Result.AppendLine(TAB4 + "var item = new " + TblOne.Name);
            Result.AppendLine(TAB4 + "{");

            for (int i = 0; i < TblOne.Attributes.Count; i++)
            {
                if (!TblOne.Attributes[i].IsPrimaryKey || (TblOne.Attributes[i].IsPrimaryKey && !TblOne.Attributes[i].IsIdentify))
                {
                    if (TblOne.Attributes[i].Type == DataType.DATETIME)
                    {
                        Result.AppendLine(TAB5 + TblOne.Attributes[i].Name + " = dt" + TblOne.Attributes[i].Name + ".Value,");
                    }
                    else if (TblOne.Attributes[i].Type == DataType.BOOL)
                    {
                        Result.AppendLine(TAB5 + TblOne.Attributes[i].Name + " = cbx" + TblOne.Attributes[i].Name + ".SelectedIndex == 0 ? true : false,");
                    }
                    else
                    {
                        if (TblOne.Attributes[i].Type == DataType.STRING)
                        {
                            Result.AppendLine(TAB5 + TblOne.Attributes[i].Name + " = txt" + TblOne.Attributes[i].Name + ".Text,");
                        }
                        else if (TblOne.Attributes[i].Type == DataType.IMAGE)
                        {
                            found = true;
                        }
                        else
                        {
                            Result.AppendLine(TAB5 + TblOne.Attributes[i].Name + " = " + Utils.GetDataType(TblOne.Attributes[i].Type) + ".Parse(txt" + TblOne.Attributes[i].Name + ".Text),");
                        }
                    }
                }
            }

            Result.AppendLine(TAB4 + "};");

            if (found)
            {
                for (int i = 0; i < TblOne.Attributes.Count; i++)
                {
                    if (TblOne.Attributes[i].Type == DataType.IMAGE)
                    {
                        Result.AppendLine(TAB4 + "if(openFileDialog1.FileName != \"\")");
                        Result.AppendLine(TAB4 + "{");
                        Result.AppendLine(TAB5 + "item." + TblOne.Attributes[i].Name + " = FileHelpers.ReadFile(openFileDialog1.FileName);");
                        Result.AppendLine(TAB4 + "}");
                        Result.AppendLine(TAB4 + "else");
                        Result.AppendLine(TAB4 + "{");
                        Result.AppendLine(TAB5 + "item." + TblOne.Attributes[i].Name + " = null;");
                        Result.AppendLine(TAB4 + "}");
                    }
                }
            }

            Result.AppendLine(TAB4 + "if (!" + TblOne.Name + "Repository.Insert(item))");
            Result.AppendLine(TAB4 + "{");
            Result.AppendLine(TAB5 + "return false;");
            Result.AppendLine(TAB4 + "}");
            Result.AppendLine(TAB4 + "return true;");
            Result.AppendLine(TAB3 + "}");
            Result.AppendLine(TAB3 + "catch");
            Result.AppendLine(TAB3 + "{");
            Result.AppendLine(TAB4 + "return false;");
            Result.AppendLine(TAB3 + "}");
            Result.AppendLine(TAB2 + "}");

            return Result.ToString();
        }

        public string GenerateActionUpdate()
        {
            StringBuilder Result = new StringBuilder();
            var found = false;

            Result.AppendLine(TAB2 + "private bool ActionUpdate()");
            Result.AppendLine(TAB2 + "{");
            Result.AppendLine(TAB3 + "try");
            Result.AppendLine(TAB3 + "{");
            Result.AppendLine(TAB4 + "var item = " + TblOne.Name + "Repository.SelectByID(int.Parse(txt" + Utils.GetPK(TblOne).Name + ".Text));");

            for (int i = 0; i < TblOne.Attributes.Count; i++)
            {
                if (!TblOne.Attributes[i].IsPrimaryKey || (TblOne.Attributes[i].IsPrimaryKey && !TblOne.Attributes[i].IsIdentify))
                {
                    if (TblOne.Attributes[i].Type == DataType.DATETIME)
                    {
                        Result.AppendLine(TAB4 + "item." + TblOne.Attributes[i].Name + " = dt" + TblOne.Attributes[i].Name + ".Value;");
                    }
                    else if (TblOne.Attributes[i].Type == DataType.BOOL)
                    {
                        Result.AppendLine(TAB4 + "item." + TblOne.Attributes[i].Name + " = cbx" + TblOne.Attributes[i].Name + ".SelectedIndex == 0 ? true : false;");
                    }
                    else
                    {
                        if (TblOne.Attributes[i].Type == DataType.STRING)
                        {
                            Result.AppendLine(TAB4 + "item." + TblOne.Attributes[i].Name + " = txt" + TblOne.Attributes[i].Name + ".Text;");
                        }
                        else if (TblOne.Attributes[i].Type == DataType.IMAGE)
                        {
                            found = true;
                        }
                        else
                        {
                            Result.AppendLine(TAB4 + "item." + TblOne.Attributes[i].Name + " = " + Utils.GetDataType(TblOne.Attributes[i].Type) + ".Parse(txt" + TblOne.Attributes[i].Name + ".Text);");
                        }
                    }
                }
            }

            if (found)
            {
                for (int i = 0; i < TblOne.Attributes.Count; i++)
                {
                    if (TblOne.Attributes[i].Type == DataType.IMAGE)
                    {
                        Result.AppendLine(TAB4 + "if(openFileDialog1.FileName != \"\")");
                        Result.AppendLine(TAB4 + "{");
                        Result.AppendLine(TAB5 + "item." + TblOne.Attributes[i].Name + " = FileHelpers.ReadFile(openFileDialog1.FileName);");
                        Result.AppendLine(TAB4 + "}");
                        Result.AppendLine(TAB4 + "else");
                        Result.AppendLine(TAB4 + "{");
                        Result.AppendLine(TAB5 + "item." + TblOne.Attributes[i].Name + " = null;");
                        Result.AppendLine(TAB4 + "}");
                    }
                }
            }

            Result.AppendLine(TAB4 + "return " + TblOne.Name + "Repository.Save();");
            Result.AppendLine(TAB3 + "}");
            Result.AppendLine(TAB3 + "catch");
            Result.AppendLine(TAB3 + "{");
            Result.AppendLine(TAB4 + "return false;");
            Result.AppendLine(TAB3 + "}");
            Result.AppendLine(TAB2 + "}");

            return Result.ToString();
        }

        public string GenerateActionDelete()
        {
            StringBuilder Result = new StringBuilder();

            Result.AppendLine(TAB2 + "private bool ActionDelete()");
            Result.AppendLine(TAB2 + "{");
            Result.AppendLine(TAB3 + "try");
            Result.AppendLine(TAB3 + "{");
            Result.AppendLine(TAB4 + "return " + TblOne.Name + "Repository.Delete(int.Parse(txt" + Utils.GetPK(TblOne).Name + ".Text));");
            Result.AppendLine(TAB3 + "}");
            Result.AppendLine(TAB3 + "catch");
            Result.AppendLine(TAB3 + "{");
            Result.AppendLine(TAB4 + "return false;");
            Result.AppendLine(TAB3 + "}");
            Result.AppendLine(TAB2 + "}");

            return Result.ToString();
        }
    }
}
